﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Small.Excel
{
    public static class ConvertProperty
    {

        public static void SetThisValue<T>(T t, String fieldName, object value)
        {


            if (Convert.IsDBNull(value)) { return; }

            Type type = t.GetType().GetProperty(fieldName).PropertyType;
            if (type == typeof(String))
            {
                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToString(value), null);
            }
            else if (type == typeof(Double) || type == typeof(double))
            {
                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToDouble(value), null);

            }
            else if (type == typeof(Int32) || type == typeof(int) || type == typeof(uint)
                    || type == typeof(UInt16) || type == typeof(UInt64)
                    || type == typeof(UInt32)
                    || type == typeof(Int16)
                    || type == typeof(UInt64)
                   )
            {

                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToInt32(value), null);
            }
            else if (type == typeof(float))
            {

                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToSingle(value), null);
            }
            else if (type == typeof(Guid))
            {


                t.GetType().GetProperty(fieldName).SetValue(t, Guid.Parse(Convert.ToString(value)), null);
            }
            else if (type == typeof(Byte) || type == typeof(byte))
            {

                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToByte(value), null);
            }
            else if (type == typeof(Boolean) || type == typeof(bool))
            {

                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToBoolean(value), null);
            }
            else if (type == typeof(char) || type == typeof(Char))
            {

                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToChar(value), null);
            }
            else if (type == typeof(DateTime))
            {

                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToDateTime(value), null);
            }
            else if (type == typeof(Decimal))
            {

                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToDecimal(value), null);
            }
            else if (type == typeof(long))
            {

                t.GetType().GetProperty(fieldName).SetValue(t, Convert.ToInt64(value), null);
            }
            else
            {

                throw new Exception("unkown type");
            }
        }
    }
}
